home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / MoofWars / MoofEncoder / Color Search Procs.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  3.7 KB  |  142 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Color Search Procs.cp
  3.  
  4.     Contains:    This file defines a number of commonly used color search procs.  For the encoder,
  5.                    we only use one of the procs, which returns white if the color of a pixel is within
  6.                 a certain delta of a key color, and returns black for all other cases.  We can use
  7.                 this to quickly create masks of an image.
  8.  
  9.     Written by: Timothy Carroll    
  10.  
  11.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  12.  
  13.                 You may incorporate this Apple sample source code into your program(s) without
  14.                 restriction. This Apple sample source code has been provided "AS IS" and the
  15.                 responsibility for its operation is yours. You are not permitted to redistribute
  16.                 this Apple sample source code as "Apple sample source code" after having made
  17.                 changes. If you're going to re-distribute the source, we require that you make
  18.                 it clear in the source that the code was descended from Apple sample source
  19.                 code, but that you've made changes.
  20.  
  21.     Change History (most recent first):
  22.                 7/1/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  23.                 8/15/96        Timothy Carroll    Initial Release
  24.                 
  25.  
  26. */
  27.  
  28.  
  29. #include "Color Search Procs.h"
  30.  
  31. SInt32        gSearchDelta = 0x2000;
  32. RGBColor    gMaskColor = {0,0,0};
  33.  
  34. // We're going to define a custom color search proc.  This will be used by the class routines
  35. // to create the mask.
  36.  
  37. static pascal Boolean    MaskSearchProc (RGBColor *theColor, long *result);
  38. static pascal Boolean    LightenSearchProc (RGBColor *theColor, long *result);
  39. static pascal Boolean   DarkenSearchProc (RGBColor *theColor, long *result);
  40.  
  41. // We want to be able to compile for powermacs, so we'll provide UPPs for all
  42. // of the custom search procs. Note that we don't actually allow passing
  43. // the search proc's directly, only the UPPs.
  44. ColorSearchUPP MaskSearchProcUPP = NewColorSearchProc(MaskSearchProc);
  45. ColorSearchUPP LightenSearchProcUPP = NewColorSearchProc(LightenSearchProc);
  46. ColorSearchUPP DarkenSearchProcUPP = NewColorSearchProc(DarkenSearchProc);
  47.  
  48.  
  49. // The Mask search proc needs to be able to pass in a specific color to be used as
  50. // the mask.  We can't change the calling parameters or provide it via a handle
  51. // (unlike some System 7 routines, this one doesn't pass any "user data".). 
  52. // So any additional parameters, like our key color and the delta, need to be
  53. // defined and passed in as global variables.
  54.  
  55.  
  56. pascal Boolean MaskSearchProc (RGBColor *theColor, long *result)
  57. {
  58.     long temp;
  59.     if (theColor->red > gMaskColor.red)
  60.     {
  61.         temp = theColor->red - gMaskColor.red;
  62.         if (temp > gSearchDelta)
  63.         {
  64.             *theColor = kBlack;
  65.             return false;
  66.         }
  67.     }
  68.     else
  69.     {
  70.         temp = gMaskColor.red - theColor->red;
  71.         if (temp > gSearchDelta)
  72.         {
  73.             *theColor = kBlack;
  74.             return false;        
  75.         }
  76.     }
  77.     
  78.     if (theColor->green > gMaskColor.green)
  79.     {
  80.         temp = theColor->green - gMaskColor.green;
  81.         if (temp > gSearchDelta)
  82.         {
  83.             *theColor = kBlack;
  84.             return false;
  85.         }
  86.     }
  87.     else
  88.     {
  89.         temp = gMaskColor.green - theColor->green;
  90.         if (temp > gSearchDelta)
  91.         {
  92.             *theColor = kBlack;
  93.             return false;        
  94.         }
  95.     }
  96.     
  97.     if (theColor->blue > gMaskColor.blue)
  98.     {
  99.         temp = theColor->blue - gMaskColor.blue;
  100.         if (temp > gSearchDelta)
  101.         {
  102.             *theColor = kBlack;
  103.             return false;
  104.         }
  105.     }
  106.     else
  107.     {
  108.         temp = gMaskColor.blue - theColor->blue;
  109.         if (temp > gSearchDelta)
  110.         {
  111.             *theColor = kBlack;
  112.             return false;        
  113.         }
  114.     }
  115.     
  116.     *theColor = kWhite;
  117.     return false;
  118.  
  119. #pragma unused (result)
  120. }
  121.  
  122.  
  123. pascal Boolean DarkenSearchProc (RGBColor *theColor, long *result)
  124. {
  125.     (theColor->red) >>= 1;
  126.     (theColor->green) >>= 1;
  127.     (theColor->blue) >>= 1;
  128.     
  129.     return false;
  130. #pragma unused (result)
  131. }
  132.  
  133.  
  134. pascal Boolean LightenSearchProc (RGBColor *theColor, long *result)
  135. {
  136.     (theColor->red) <<= 1;
  137.     (theColor->green) <<= 1;
  138.     (theColor->blue) <<= 1;
  139.  
  140.     return false;
  141. #pragma unused (result)
  142. }